

#include <Conveyor.h>
#include <EllipseItem.h>
#include <gui\painting\qbrush.h>



namespace PackSim
{

	Conveyor::Conveyor( QGraphicsItem* parent )
	:QGraphicsRectItem(parent)
	{
		mVelocity = 0.1;
		mAcceleration = 0.01;
		mActualVelocity = 0;
		mColor = Qt::gray;
		mConveyorState = eCsStopped;
	}


	Conveyor::~Conveyor()
	{
		//mItemList.clear();
	}


	void Conveyor::startCnv()
	{
		mConveyorState = eCsStarted;
	}

	void Conveyor::stopCnv()
	{
		mConveyorState = eCsStopped;
	}

	void Conveyor::updateCnv(float aDeltaTime)
	{
		

		float speedChange = aDeltaTime * 0.001F * mAcceleration;
		if (eCsStopped == mConveyorState)
		{
			if(qAbs(mActualVelocity) <= speedChange)
			{
				mActualVelocity = 0;
			}
			else
			{
				mActualVelocity -= speedChange;
			}
		}

		else if( (eCsStarted == mConveyorState) &&  (qAbs( qAbs(mVelocity)- qAbs(mActualVelocity) ) > speedChange ) )
		{
			if(mActualVelocity <= mVelocity)
			{
				mActualVelocity += speedChange;
			}
			else
			{
				mActualVelocity -= speedChange;
			}
		}

		updateCnvItems(aDeltaTime);

		
		/*for(int i = 0; i < 100000; i++)
		{
			int x = i*i*i*i;
			x = x/(i+5);
			x = x+3;
		}*/

		
	}

	void Conveyor::updateCnvItems(float aDeltaTime)
	{
		QList<QGraphicsItem*> items = childItems();


		foreach(QGraphicsItem* itm, items)
		{
			itm->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
			//itm->pos().x() > this->rect().width();

			//this->rect().width();
			//this->rect().width();
			//this->rect().width();
			//this->rect().width();
			//this->rect().width();
			//this->rect().width();
			//this->rect().width();
			//this->rect().width();
			//this->rect().width();


			if(itm->pos().x() > this->rect().width() )
			{
				emit ConveyorSignalItemEndOfCnv(this, (EllipseItem*)itm);
			}
		
			
			/*if (!contains(items[i]->pos()))
			{
				emit ConveyorSignalItemEndOfCnv(this, (EllipseItem*)items.at(i));
			}*/
		}


		//for(int i = 0; i < items.length(); i++)
		//{
		//	/*QPointF position = items[i]->pos();
		//	position.setX(position.x()+mActualVelocity* 0.001F *gcDELTA_TIME);
		//	
		//	items[i]->setPos(position);*/

		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
	
		///*	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(-mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);
		//	items[i]->moveBy(mActualVelocity* 0.001F *aDeltaTime,0);*/
		//	
		//	if(items[i]->pos().x() > this->rect().width() )
		//	{
		//		emit ConveyorSignalItemEndOfCnv(this, (EllipseItem*)items.at(i));
		//	}
		//	
		//	/*if (!contains(items[i]->pos()))
		//	{
		//		emit ConveyorSignalItemEndOfCnv(this, (EllipseItem*)items.at(i));
		//	}*/

		//}

	}


	float Conveyor::velocity() const 
	{ 
		return mVelocity; 
	}
	
	void Conveyor::setVelocity(float val) 
	{ 
		mVelocity = val; 
	}

	float Conveyor::acceleration() const 
	{ 
		return mAcceleration; 
	}
	
	void Conveyor::setAcceleration(float val) 
	{ 
		mAcceleration = qAbs(val); 
	}

	
	void Conveyor::setRotation( double angle )
	{
		QGraphicsRectItem::setRotation(angle);
	}

	double Conveyor::rotation() const
	{
		return QGraphicsRectItem::rotation();
	}


	void Conveyor::setPos( double x, double y )
	{
		QGraphicsRectItem::setPos(x,y);
		
	}

	void Conveyor::setPos( const QPointF & pos )
	{
		QGraphicsRectItem::setPos(pos);
	}

	QPointF Conveyor::pos() const
	{
		return QGraphicsRectItem::pos();
	}

	void Conveyor::setRect( const QRectF & rectangle )
	{
		QGraphicsRectItem::setRect(rectangle);
	}

	void Conveyor::setRect( double x, double y, double width, double height )
	{
		QGraphicsRectItem::setRect(x,y,width,height);
	}

	QRectF Conveyor::rect() const
	{
		return QGraphicsRectItem::rect();
	}

	void Conveyor::setColor( QColor& color )
	{
		mColor = color;
		QBrush brush(color);
		setBrush(brush);
	}

	QColor Conveyor::color() const
	{
		return mColor;
	}

	void Conveyor::appendNextConveyor( Conveyor* aConveyor )
	{
		aConveyor->appendPreviousConveyor(this);
		mNextConveyorList.append(aConveyor);
	}

	void Conveyor::removeNextConveyor( Conveyor* aConveyor )
	{
		aConveyor->removePreviousConveyor(this);
		mNextConveyorList.removeOne(aConveyor);
	}

	void Conveyor::appendPreviousConveyor( Conveyor* aConveyor )
	{
		mPreviousConveyorList.append(aConveyor);
	}

	void Conveyor::removePreviousConveyor( Conveyor* aConveyor )
	{
		mPreviousConveyorList.removeOne(aConveyor);
	}

	QList<Conveyor*> Conveyor::nextConveyorList() const 
	{
		return mNextConveyorList; 
	}

	QList<Conveyor*> Conveyor::previousConveyorList() const 
	{
		return mPreviousConveyorList; 
	}
	

}